package com.accp.jboa.biz;

import com.accp.jboa.dao.ReimburseDetailMapper;
import com.accp.jboa.dao.ReimburseMapper;
import com.accp.jboa.pojo.Reimburse;
import com.accp.jboa.pojo.ReimburseDetail;
import com.accp.jboa.pojo.Status;
import com.accp.jboa.vo.ReimburseAndEmployeeAndDetail;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service("reimbursebiz")
public class Reimbursebiz {

    @Autowired(required = false)
    private ReimburseMapper dao;

    @Autowired(required = false)
    private ReimburseDetailMapper dao2;

    public int insertSReimburse(Reimburse[] reimburse) {
        int insert = 0;
        for (Reimburse r : reimburse) {
            insert = dao.insert(r);
            if (insert != 1) {
                return insert;
            }
        }
        return insert;
    }

    public PageInfo<ReimburseAndEmployeeAndDetail> queryAll(Integer page, Integer size, String starttime, String endtime,String status) {
        PageHelper.startPage(page, size);
        if (Objects.equals(starttime,"undefined")){
            starttime = null;
        }
        if (Objects.equals(endtime,"undefined")){
            endtime = null;
        }
        if (Objects.equals(status,"undefined")) {
            status = null;
        }
        return new PageInfo<>(dao.queryAll(starttime,endtime,status));
    }

    public List<Status> queryAllStatus() {
        return dao.queryAllStatus();
    }

    public ReimburseAndEmployeeAndDetail queryByid(Integer reimburseid) {
        return dao.queryByid(reimburseid);
    }

    public int update(ReimburseDetail[] reimburseDetail) {
        int insert = 0;
        for (ReimburseDetail r : reimburseDetail) {
            Reimburse reimburse = new Reimburse();
            reimburse.setReimburseid(r.getMainid());
            reimburse.setNextdealman(r.getNextdealman());
            System.out.println(r.getNextdealman()+"---------------------------");
            reimburse.setStatusid(r.getStatusid());
            dao.updateByPrimaryKeySelective(reimburse);
            if (r.getId()!= null){
                insert=dao2.updateByPrimaryKeySelective(r);
            }else {
                insert = dao2.insertSelective(r);
            }
        }
        return insert;
    }


    public Reimburse insert(ReimburseDetail[] reimburseDetail) {
        Reimburse reimburse = new Reimburse();
        reimburse.setCreatetime(reimburseDetail[0].getCreatetime());
        reimburse.setCreateman(reimburseDetail[0].getCreateman());
        reimburse.setNextdealman(reimburseDetail[0].getNextdealman());
        reimburse.setDepartmentid(reimburseDetail[0].getDepartmentid());
        reimburse.setEvent(reimburseDetail[0].getEvent());
        reimburse.setTotalcount(reimburseDetail[0].getTotalcount());
        reimburse.setStatusid(1);
        reimburse.setTotalcount(reimburseDetail[0].getTotalcount());
        dao.insert(reimburse);
        for (int i = 2; i < reimburseDetail.length; i++) {
            ReimburseDetail r = reimburseDetail[i];
            r.setMainid(reimburse.getReimburseid());
            r.setId(null);
            System.out.println("------------------");
            System.out.println(r);
            dao2.insertSelective(r);
        }
        return reimburse;
    }
}
